package com.acmen.easyapi.web;


import com.acmen.easyapi.model.Response;
import com.acmen.easyapi.util.RequestUtil;
import com.alibaba.fastjson.JSON;
import com.acmen.easyapi.service.ApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author acmen
 * 接口文档服务
 * @date 2022-03-16
 */
@RestController
public class ApiController {
    @Autowired
    private ApiService apiService;

    @RequestMapping(value = "/api/{apiId}", method = RequestMethod.GET)
    public ModelAndView getApiDetail(HttpServletRequest request, ModelMap model, @PathVariable String apiId) {
        return apiService.apiDetail(request,apiId,model);
    }

    @PostMapping(value = "/api/delete")
    public Response delete(HttpServletRequest request,@RequestParam("apiIds") String apiIds) {
        String ip = RequestUtil.getIpAddress(request);
        return apiService.delApi(ip,Arrays.asList(apiIds.split(",")));
    }
}
